package com.iseastar.guojiang.team.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.model.CourierTeamMonthBean;
import com.iseastar.guojiang.team.TeamCenterZoneListActivity;
import com.shouhuobao.station.R;
import com.shouhuobao.take.weight.CircularImage;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * 运营中战队
 *
 * @author 李澄锋<br>
 * 2018/6/13
 */
public class OperationTeamZoneAdapter extends BaseAdapterExt<CourierTeamMonthBean> {

    public OperationTeamZoneAdapter(ArrayList<CourierTeamMonthBean> items, Activity context) {
        super(items, context);
        setEmptyView(R.layout.courier_team_center_list_empty);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder viewHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_courier_team_zone_list, parent, false);
            viewHolder.teamNameTV = convertView.findViewById(R.id.team_name_tv);
            viewHolder.teamAchievementTV = convertView.findViewById(R.id.team_achievement_tv);
            viewHolder.teamMemberNumTV = convertView.findViewById(R.id.team_num_tv);
            viewHolder.joinTeamTV = convertView.findViewById(R.id.join_team_tv);
            viewHolder.teamLeaderIV = convertView.findViewById(R.id.team_leader_iv);
            viewHolder.teamLeaderInfoTV = convertView.findViewById(R.id.team_leader_info_tv);
            viewHolder.teamRankTV = convertView.findViewById(R.id.team_rank_tv);
            viewHolder.lookTeamTV = convertView.findViewById(R.id.look_team_tv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final CourierTeamMonthBean item = getItem(position);
        viewHolder.teamNameTV.setText(item.getTeamName());
        viewHolder.teamAchievementTV.setText("累计帮助" + item.getOrderCntTotal() + "个用户完成配送");
        if (item.getOtherMaxCourierNum() == 0 || item.getJoinStatus() == 2) {
            viewHolder.joinTeamTV.setBackgroundResource(R.drawable.app_courier_c7c7c7_3_bg);
            if (item.getOtherMaxCourierNum() == 0) {
                viewHolder.teamMemberNumTV.setText("战队已满员");
                viewHolder.joinTeamTV.setText("已满员");
            } else {
                viewHolder.teamMemberNumTV.setText("再有" + item.getOtherMaxCourierNum() + "人满员");
            }
            if (item.getJoinStatus() == 2) {
                viewHolder.joinTeamTV.setText("加入");
            }
        } else {
            viewHolder.teamMemberNumTV.setText("再有" + item.getOtherMaxCourierNum() + "人满员");
            viewHolder.joinTeamTV.setBackgroundResource(R.drawable.same_city_complete_the_task_6783d8_bg);
            viewHolder.joinTeamTV.setText("加入");
        }
        viewHolder.lookTeamTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMgr.courierTeamDetailsDialog(context, item.getImagePath(), item.getNickName(), item.getName(), item.getTeamSlogan(), item.getTeamIntroduce());
            }
        });
        GlideUtil.loadImage(context, item.getImagePath(), R.drawable.setting_head_hover_default, viewHolder.teamLeaderIV);
        viewHolder.teamLeaderIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMgr.courierTeamDetailsDialog(context, item.getImagePath(), item.getNickName(), item.getName(), item.getTeamSlogan(), item.getTeamIntroduce());
            }
        });
        if (Str.isEmpty(item.getName())) {
            viewHolder.teamLeaderInfoTV.setText(item.getNickName() + "（）");
        } else {
            viewHolder.teamLeaderInfoTV.setText(item.getNickName() + "（" + item.getName().substring(0, 1) + "师傅）");
        }
        viewHolder.teamRankTV.setText((position + 1) + "");
        //点击申请加入战队
        viewHolder.joinTeamTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1:审核中，2：已加入，3：未加入
                if (item.getOtherMaxCourierNum() > 0) {
                    if (item.getJoinStatus() == 1) {
                        if (item.getJoinTeamId() == item.getTeamId()) {
                            ((TeamCenterZoneListActivity) context).showToast("您已经提交过加入申请了哦！");
                        } else {
                            DialogMgr.courierDuplicateClaimDialog((TeamCenterZoneListActivity) context, item.getJoinTeamName(), item.getTeamId());
                        }
                    } else if (item.getJoinStatus() == 3) {
                        DialogMgr.courierNoExitForTwoMonthsDialog((TeamCenterZoneListActivity) context, item.getTeamId());
                    }
                }
            }
        });
        return convertView;
    }

    private class ViewHolder {
        TextView teamNameTV;//战队名称
        TextView teamAchievementTV;//战队成就
        TextView teamMemberNumTV;//战队成员数量
        TextView joinTeamTV;//加入战队申请
        CircularImage teamLeaderIV;//队长头像
        TextView teamLeaderInfoTV;//队长信息
        TextView teamRankTV;//战队排名
        TextView lookTeamTV;//查看战队介绍

    }
}
